<template>
	<view>
		<u-form v-if="inited" ref="formRef" labelPosition="left" :model="form" :rules="rules">
			<view class="title">
				<text>任务信息</text>
			</view>
			<view class="form_group">
				<z-input-picker keyName="label" label="任务类型:" labelWidth="90" prop="samplingType" :disabled="!p2Edit" :list="samplingTypeList" :required="true"
					:showPicker="showSamplingTypePicker" :value="form.samplingTypeName" @cancel="showSamplingTypePicker = false" @confirm="setSamplingType"
					@open="() => {if(p2Edit){ showSamplingTypePicker = true }}"></z-input-picker>
				<z-input label="点位编码:" labelWidth="90" prop="code" :disabled="true" :readonly="true" :required="true" :value="form.code"
					@change="(val) => setVal('task', 'code', val)"></z-input>
			</view>
			<view class="title">
				<text>点位区域</text>
				<text v-if="p2Edit" style="color: #1296db;" @click="getAddress">获取乡镇村</text>
			</view>
			<view class="form_group">
				<z-input label="省:" labelWidth="90" prop="provinceName" :disabled="!p2Edit" :required="true" :value="form.provinceName"
					@change="(val) => setVal('task', 'provinceName', val)"></z-input>
				<z-input label="市:" labelWidth="90" prop="cityName" :disabled="!p2Edit" :required="true" :value="form.cityName"
					@change="(val) => setVal('task', 'cityName', val)"></z-input>
				<z-input label="区/县:" labelWidth="90" prop="countyName" :disabled="!p2Edit" :required="true" :value="form.countyName"
					@change="(val) => setVal('task', 'countyName', val)"></z-input>
				<z-input label="乡镇:" labelWidth="90" prop="town" :disabled="!p2Edit" :required="true" :value="form.town"
					@change="(val) => setVal('task', 'town', val)"></z-input>
				<z-input label="村:" labelWidth="90" prop="village" :disabled="!p2Edit" :required="true" :value="form.village"
					@change="(val) => setVal('task', 'village', val)"></z-input>
				<z-input label="组:" labelWidth="90" prop="team" :disabled="!p2Edit" :required="false" :value="form.team"
					@change="(val) => setVal('task', 'team', val)"></z-input>
			</view>
		</u-form>
		<u-notify ref="notify"></u-notify>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import NotifyMix from '@/mixins/notify-fix.js'
	import Util from '@/utils/index.js'
	export default {
		mixins: [NotifyMix],
		data() {
			return {
				form: {
					samplingType: '', // 任务类型
					samplingTypeName: '',
					code: "", // 点位编码
					provinceName: "", // 地址
					cityName: "", // 地址
					countyName: "", // 地址
					town: '', // 乡镇
					village: "", // 村
					team: '' // 组
				},
				rules: {
					'samplingType': {
						required: true,
						message: '请选择任务类型',
						trigger: ['change'],
					},
					"provinceName": {
						required: true,
						message: '请输入省',
						trigger: ['change'],
					},
					"cityName": {
						required: true,
						message: '请输入市',
						trigger: ['change'],
					},
					"countyName": {
						required: true,
						message: '请输入区/县',
						trigger: ['change'],
					},
					"town": {
						required: true,
						message: '请输入点位所在乡镇',
						trigger: ["change"],
					},
					"village": {
						required: true,
						message: '请输入点位所在村庄',
						trigger: ['change'],
					}
				},
				showSamplingTypePicker: false,
				samplingTypeList: [],
				isAddressing: false,
				inited: false
			}
		},
		computed: {
			...mapState({
				action: state => state.cjRecord.action,
				taskInfo: state => state.cjRecord.taskInfo,
				recordInfo: state => state.cjRecord.recordInfo,
				isLT200m: state => state.cjRecord.isLT200m,
				userInfo: state => state.user.userInfo
			}),
			p1Edit() {
				return ['edit', 'add'].includes(this.action) && this.taskInfo.positionType === '1' && this.isLT200m
			},
			p2Edit() {
				console.log(this.taskInfo.positionType,'this.taskInfo.positionType')
				return ['edit', 'add'].includes(this.action) && this.taskInfo.positionType === '2'
			},
			canEdit() {
				return this.p1Edit || this.p2Edit
			},
			cantEdit() {
				return !this.canEdit
			}
		},
		methods: {
			...mapMutations({
				setTaskInfoAttr: 'cjRecord/setTaskInfoAttr',
				setRecordInfoAttr: 'cjRecord/setRecordInfoAttr'
			}),
			setVal(target, key, val) {
				this.form[key] = val
				let d = {
					key,
					val
				}
				if (target === 'task') {
					this.setTaskInfoAttr(d)
				} else if (target === 'record') {
					this.setRecordInfoAttr(d)
				}
			},
			setSamplingType(val) {
                console.log("val = ",val)
				this.form.samplingType = val.value
				this.form.samplingTypeName = val.label
				this.setTaskInfoAttr({
					key: 'samplingType',
					val: val.value
				})
				this.showSamplingTypePicker = false
			},
			async getAddress() {
				this.isAddressing = true
				uni.showLoading()
				const res = await Util.UniUtil.getAddress()
				uni.hideLoading()
				this.isAddressing = false
				if (!res) {
					this.notifyFail(`获取地理位置失败`)
					return
				}
				this.setVal('record', 'provinceName', res[0])
				this.setVal('record', 'cityName', res[1])
				this.setVal('record', 'countyName', res[2])
				this.setVal('task', 'town', res[3])
				this.setVal('task', 'village', res[4])
			},
			async checkForm() {
				try {
					await this.$refs.formRef.validate()
					return true
				} catch (e) {
					return false
				}
			}
		},
		mounted() {
			Util.ObjUtil.assign(this.taskInfo, this.form)
			Util.ObjUtil.assign(this.recordInfo, this.form)
			if (!Util.StringUtil.isEmpty(this.taskInfo.samplingType)) {
				this.form.samplingTypeName = Util.DictUtil.getLabel('cj_sampling_type', this.taskInfo.samplingType)
			}
			this.samplingTypeList = Util.DictUtil.getDict('cj_sampling_type')
			this.inited = true
		}
	}
</script>

<style>
</style>